<?php
/**
 * URI View Helper.
 * @package halo
 */

hc_core_ClassLoader::load('hc_uri_Uri');

/**
 * URI View Helper.
 *
 * Provides access to the URIs for the application. Allows template creators to
 * abstractly refer to URIs to other sites within the application.
 *
 * <code>
 * // Outputs: /profile/5
 * print $uri->get('/profile/:profileId', array('profileId' => 5));
 *
 * // Outputs: /profile/62?sort=reverse
 * print $uri->get('/profile/:profileId', array('profileId' => 62, 'sort' => 'reverse'));
 *
 * // Outputs: /profile?profileId=132&sort=reverse
 * print $uri->get('/profile', array('profileId' => 132, 'sort' => 'reverse'));
 * </code>
 * @package halo
 */
class halo_UriViewHelper {

    /**
     * @var halo_HttpRequest
     */
    private $httpRequest;

    /**
     * @var halo_HttpResponse
     */
    private $httpResponse;

    /**
     * @var hc_uri_Uri
     */
    private $hcUri;

    /**
     * Constructor
     * @param halo_HttpRequest $httpRequest The request.
     * @param halo_HttpResponse $httpResponse The response.
     */
    public function __construct(halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse) {
        $this->httpRequest = $httpRequest;
        $this->httpResponse = $httpResponse;
        $context = halo_DispatcherUtil::GET_CONTEXT($this->httpRequest);
        $uriConfiguration = $context->get('uriConfiguration');
        $this->hcUri = new hc_uri_Uri($uriConfiguration, $httpRequest->getScriptPathRoot());
    }

    /**
     * Get the URI base.
     *
     * Gets the absolute URI to the base of the active site.
     * @return string Absolute URI to base of active site.
     */
    public function getBase() {
        return $this->getAbsolute('');
    }

    /**
     * Get a URI for the active site.
     * @param string $path Path.
     * @param array $options Options.
     * @param array $remember Array of params to remember for this URI.
     * @param boolean $secure Secure URI?
     */
    public function get($path, $options = null, $remember = null, $secure = null) {
        $args = func_get_args();
        while ( count($args) < 4 ) {
            $args[] = null;
        }
        $args = array_merge($args, $this->getAllRequestParams());
        return call_user_func_array(array($this->hcUri, 'get'), $args);
    }

    /**
     * Get an absolute URI for the active site.
     * @param string $path Path.
     * @param array $options Options.
     * @param array $remember Array of params to remember for this URI.
     * @param boolean $secure Secure URI?
     */
    public function getAbsolute($path, $options = null, $remember = null, $secure = null) {
        $args = func_get_args();
        while ( count($args) < 4 ) {
            $args[] = null;
        }
        $args = array_merge($args, $this->getAllRequestParams());
        return call_user_func_array(array($this->hcUri, 'getAbsolute'), $args);
    }

    /**
     * Get a site URI.
     * @param string $site Site name.
     * @param string $path Path.
     * @param array $options Options.
     * @param array $remember Array of params to remember for this URI.
     * @param boolean $secure Secure URI?
     */
    public function getSite($site, $path, $options = null, $remember = null, $secure = null) {
        $args = func_get_args();
        while ( count($args) < 5 ) {
            $args[] = null;
        }
        $args = array_merge($args, $this->getAllRequestParams());
        return call_user_func_array(array($this->hcUri, 'getSite'), $args);
    }

    /**
     * Get all params that are a part of the request.
     * @return array All request params.
     */
    protected function getAllRequestParams() {
        return array(
            $this->httpRequest->getQueryParams(),
            $this->httpRequest->getPostParams(),
            $this->httpRequest->getUrlParams()
        );
    }
}
?>
